/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.lastproject;
import Server.Helper.StringHelper;
import Server.MyDataType.Attribute_Value;
import Server.MyDataType.MyDataDynamic;
import Server.MyDataType.MyRowData;
import Server.MyDataType.MyStructTable;
import Server.MyDataType.MyTable;
import Server.SQL.MyOperand;
import Server.SQL.TypeData;
import com.sun.org.apache.xml.internal.utils.DOMBuilder;
import com.sun.org.apache.xpath.internal.NodeSet;
import org.w3c.dom.Document;
import java.io.*;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import sun.misc.Sort;
/**
 *
 * @author CANG
 */

public class XMLManage 
{
    public static final String FILE_DATABASE = "database.xml";
    Document _doc = null; 
             
    public XMLManage()
    {
        File file = new File(FILE_DATABASE);
        if(!file.exists())
        {
            DocumentBuilder builder = null;
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            
            try
            {                
                builder = factory.newDocumentBuilder();
                _doc = builder.newDocument();
                Element root = (Element)_doc.createElement("LIST_TABLE");
                
                _doc.appendChild(root);                                                   
                _doc.getDocumentElement().normalize();
                
                saveToFile();                
            }
            catch(Exception ex)
            {               
                
            }
        }
        else
        {
            readFileDocument();
        }
    }
    
    public void HandleCommand_Insert(String command)
    {        
        String delimiter = " ";
        String[] type_command = command.split(delimiter);
    }
    
    public Document getDocument()
    {
        return _doc;
    }
    
    public boolean readFileDocument()
    {
        try
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();            
            factory.setValidating(false);
            factory.setNamespaceAware(false);
            
            DocumentBuilder builder = factory.newDocumentBuilder();
            _doc = builder.parse(new File(FILE_DATABASE));
            
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;        
    }
    
    public String getPrimaryKey(String nameTable)
    {
        String sPrimaryKey = null;
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            
            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                    return sPrimaryKey;
            
            sPrimaryKey = element_table.getAttribute("Primary_Key");
        }
        catch(Exception ex)
        {
            return null;
        }
        
        return sPrimaryKey;
    }
    
    public boolean existConflictPrimaryKey(String nameTable, String sPrimarykey, String sValue )
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']/ROW/" + sPrimarykey ;
            NodeList lstNodeData = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);
            if(lstNodeData != null)
            {
                int len = lstNodeData.getLength();
                for(int i = 0; i < len; i++)
                {
                    Element element = (Element)lstNodeData.item(i);
                    String value_element = element.getTextContent();
                    if(value_element.equals(sValue))
                    {
                        return true;
                    }
                }
            }

        }
        catch(Exception ex)
        {
            return false;
        }
        
        return false;
    }
    
    //sửa lại vì đã có thêm thuộc tính Id
    public boolean insertRow(String nameTable, Vector<Attribute_Value> lstValue)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            
            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                    return false;
            
            int iCount = lstValue.size();
            //kiểm tra xung đột primarykey
            String PrimaryKey = getPrimaryKey(nameTable);
            
            if(PrimaryKey != null)
            {
                for(int i = 0; i < iCount; i++)
                {
                    Attribute_Value attribute = lstValue.elementAt(i);
                    if(attribute.NameAttribute.equals(PrimaryKey))
                    {
                        String valueAttribute =  attribute.Value;
                        boolean bCheckConflict = existConflictPrimaryKey(nameTable, PrimaryKey, valueAttribute);
                        if(bCheckConflict)
                        {
                            return false;
                        }
                    }
                }
            }
            int iNewID = createID(nameTable);
            Element element_NewRow = _doc.createElement("ROW");
            element_NewRow.setAttribute("Id", String.valueOf(iNewID) );            
            
            
            for(int i = 0; i < iCount; i++)
            {                
                Attribute_Value element_attribute = lstValue.elementAt(i);
                Node item = _doc.createElement(element_attribute.NameAttribute);
                element_NewRow.appendChild(item);                                
                
                Node value = _doc.createTextNode(element_attribute.Value);
                item.appendChild(value);
            }            
            element_table.appendChild(element_NewRow);
            
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }
    
    public boolean insertRow(String nameTable, String[] lstNameColumn, MyRowData lstValue)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            
            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                    return false;
            
            String PrimaryKey = getPrimaryKey(nameTable);
            if(PrimaryKey != null)
            {
                int index_PrimaryKey =  -1;
                for(int i = 0; i < lstNameColumn.length; i++)
                {
                    if(lstNameColumn[i].equals(PrimaryKey))
                    {
                        index_PrimaryKey = i;
                        break;
                    }
                }
                if(index_PrimaryKey != -1)
                {
                    for(int i = 0; i < lstNameColumn.length; i++)
                    {
                        String value_element = lstValue._vtRow.elementAt(i);
                        boolean bCheckConflict = existConflictPrimaryKey(nameTable, PrimaryKey, value_element);
                        if(bCheckConflict)
                        {
                            return false;
                        }
                    }
                }
            }
            
            
            int iNewID = createID(nameTable);
            Element element_NewRow = _doc.createElement("ROW");
            element_NewRow.setAttribute("Id", String.valueOf(iNewID) );   
                       
            int iCount = lstValue._vtRow.size();
            for(int i = 0; i < iCount; i++)
            {
                //lstValue.
                Node item = _doc.createElement(lstNameColumn[i]);
                element_NewRow.appendChild(item);                                
                               
                Node value = _doc.createTextNode(lstValue._vtRow.elementAt(i));                
                item.appendChild(value);
            }
            element_table.appendChild(element_NewRow);
            
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }
        
    //tìm id mới định danh cho dòng mới cho bảng
    public int createID(String nameTable)
    {
        int iD = 0;
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']/ROW";
            
            NodeList element_Rows = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);            
            Vector<Integer> iID = new Vector<Integer>();
            for(int i = 0; i < element_Rows.getLength(); i++)
            {
                String id = ((Element)element_Rows.item(i)).getAttribute("Id");
                if(id != null && id != "")
                    iID.add(Integer.parseInt(id));
            }
            
           if(element_Rows.getLength() > 0)
           {
                int max = iID.elementAt(0);

                for(int j = 0; j < iID.size(); j++)
                {
                    if(iID.get(j) > max)
                        max = iID.get(j);
                }                
                 iD = max +1;
           }
        }
        catch(Exception ex)
        {
        
        }
        return iD;
    }
    
    public boolean createTable(MyStructTable structTable, String nameTable)
    {
        try
        {
            nameTable = nameTable.toUpperCase();
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            
            NodeList nodes = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);
            if(nodes != null && nodes.getLength() > 0)
                    return false;
            
            String sPrimaryKey = "null";
            Element item_table = _doc.createElement("TABLE");
            int iAmountColumn = structTable._vtAttribute.size();
            for(int i = 0; i < iAmountColumn; i++)
            {
                Attribute_Value column_value = structTable._vtAttribute.elementAt(i);
                item_table.setAttribute(column_value.NameAttribute.toUpperCase(), column_value.Value);                
            }
            
            item_table.setAttribute("Name", nameTable);
            item_table.setAttribute("Primary_Key", structTable._sPrimaryKey.toUpperCase());
            
            //tạo cấu trúc bảng
            Element element_Struct = _doc.createElement("STRUCT");
            for(int i = 0; i < iAmountColumn; i++)
            {
                Attribute_Value column_value = structTable._vtAttribute.elementAt(i);
                Element element_TypeStruct = _doc.createElement("TYPE_COLUMN");
                element_TypeStruct.setAttribute("Name", column_value.NameAttribute.toUpperCase());
                element_TypeStruct.setAttribute("Type", column_value.Type);
                element_Struct.appendChild(element_TypeStruct);
            }
            item_table.appendChild(element_Struct);
            
            Element element_root = (Element)_doc.getElementsByTagName("LIST_TABLE").item(0);
            element_root.appendChild(item_table);
            
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }
    
    public boolean insertTable(String nameTable)
    {
        try
        {            
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
                        
            NodeList nodes = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);
            if(nodes != null && nodes.getLength() > 0)
                    return false;
            
            Element item_table = _doc.createElement("TABLE");
            item_table.setAttribute("Name", nameTable);
            Element element_root = (Element)_doc.getElementsByTagName("LIST_TABLE").item(0);
            element_root.appendChild(item_table);
            
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }
    
    public boolean insertColumn(String nameTable, String nameColumn, String type )          
    {
        try
        {
            nameColumn = nameColumn.toUpperCase();
            
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            //InputSource inputSource = new InputSource(FILE_DATABASE);
            
            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
               return false;
           
            element_table.setAttribute(nameColumn, nameColumn);
            
            sExpression = "./STRUCT";
            Element element_Struct = (Element)xpath.evaluate(sExpression, element_table, XPathConstants.NODE);
            Element element_TypeStruct = _doc.createElement("TYPE_STRUCT");
            element_TypeStruct.setAttribute("Name", nameColumn );
            element_TypeStruct.setAttribute("Type", type);
            element_Struct.appendChild(element_TypeStruct);
            
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }
    
    public boolean checkExistTable(String nameTable)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            
            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
               return false;

        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }
    
    public boolean checkListColumn(String nameTable, String[] listNameColumn)
    {
        boolean bCheck = false;
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            
            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
               return false;
            
            NamedNodeMap nnMap =  element_table.getAttributes();
            int len = nnMap.getLength();
            for(int i = 0; i < listNameColumn.length; i++)
            {
                bCheck = false;
                for(int j = 0; j < len; j++)
                {
                    String attributeValue = nnMap.item(i).getNodeValue();
                    if(attributeValue.equals(listNameColumn[i]))
                    {
                        bCheck = true;
                        break;                        
                    }
                }
                if(!bCheck)
                {
                    return bCheck;
                }
            }
            
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return bCheck;
    }
    
    public String getTypeAttribute(String nameTable, String nameColumn)
    {
        nameColumn = nameColumn.toUpperCase();
        String sType = "";
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";

            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                return null;
            
            sExpression = "//STRUCT/TYPE_COLUMN[@Name = '" + nameColumn + "']";
            Element element_Attribute = (Element)xpath.evaluate(sExpression, element_table, XPathConstants.NODE);
            if(element_Attribute != null)
            {
                sType = element_Attribute.getAttribute("Type");
                return sType;
            }            
        }
        catch(Exception ex)
        {
            return null;
        }        
        return null; 
        
    }
    
    public String[] getAllNameColumn(String nameTable)
    {
        String[] lstNameColumn = null;
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";

            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                return null;
            
            NamedNodeMap lstAttribute = element_table.getAttributes();
            int len = lstAttribute.getLength();
            lstNameColumn = new String[len - 2];//-2];
            int index = 0;
            for(int i = 0; i < len; i++)
            {
                String nn = lstAttribute.item(i).getNodeName();
                if(!nn.equals("Name") && !nn.equals("Primary_Key"))
                {
                    lstNameColumn[index++] = lstAttribute.item(i).getNodeValue();
                }
            }            
            return lstNameColumn;
        }
        catch(Exception ex)
        {
            return null;
        }
       // return lstNameColumn;
    }
    
    public Vector<MyRowData> getAllRowData(String nameTable)
    {
        Vector<MyRowData> lstData = new Vector<MyRowData>();
        
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";

            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                return lstData;
            
            //lấy danh sách tên cột           
            String[] lstNameColumn = getAllNameColumn(nameTable);
            
            //lấy danh sách dữ liệu
            sExpression = "//TABLE[@Name = '" + nameTable + "']"+"/ROW";
            NodeList lstNodeData = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);            
            int iCountData = lstNodeData.getLength();
            for(int i = 0; i < iCountData; i++)
            {
                Element element_Row = (Element)lstNodeData.item(i);
                MyRowData myData = new MyRowData();                
                boolean bAdd = false;
                for(int j = 0; j < lstNameColumn.length; j++)
                {                    
                    NodeList nlElement_Data = element_Row.getElementsByTagName(lstNameColumn[j]);
                    if(nlElement_Data.getLength() > 0)
                    {
                        Element element_Data = (Element)nlElement_Data.item(0);
                        myData._vtRow.add( element_Data.getTextContent() ); 
                        bAdd = true;
                    }
                }
                myData.ID = element_Row.getAttribute("Id");
                
                if(bAdd)
                {
                    bAdd = false;
                    lstData.add(myData);
                }
            }           
        }
        catch(Exception ex)
        {
            
        }
        
        return lstData;
    }
    
    public MyDataDynamic getMyDataDynamic(String nameTable)
    {
        MyDataDynamic result = new MyDataDynamic();
        String[] lstNameColumn = getAllNameColumn(nameTable);
        for(int i = 0; i < lstNameColumn.length; i++)
        {
            result._vtNameColumn.add(lstNameColumn[i]);
        }
        result._vtRowData = getAllRowData(nameTable);        
        return result;
    }
    
    public MyDataDynamic mergeTable(MyDataDynamic myDataDynamicPrimitive, String nameTable1, MyDataDynamic myDatadynamic_Table2, String nameTable2)
    {
        
        Vector<String> vtNameColumn = new Vector<String>(0);
       
        
        //merge dữ liệu
        Vector<MyRowData> vtNewRowData = new Vector<MyRowData>();
        int AmountRowData_TablePrimitive = myDataDynamicPrimitive._vtRowData.size();
        int AmountColumn_TablePrimitive = myDataDynamicPrimitive._vtNameColumn.size();
        int AmountRowData_Table2 = myDatadynamic_Table2._vtRowData.size();
        if(AmountRowData_TablePrimitive != 0 && AmountColumn_TablePrimitive != 0 )
        {
            //thêm bảng
            vtNameColumn.addAll( myDataDynamicPrimitive._vtNameColumn );
            int len = myDatadynamic_Table2._vtNameColumn.size();
            for(int i = 0; i < len; i++ )
            {
                String nameColumn = myDatadynamic_Table2._vtNameColumn.elementAt(i);
                vtNameColumn.add( nameTable2 + "." + nameColumn);
            } 
            
            for(int i = 0; i < AmountRowData_TablePrimitive; i++)
            {               
                for(int j = 0; j < AmountRowData_Table2; j++)
                {          
                    MyRowData newRowData = new MyRowData();
                    newRowData._vtRow.addAll( myDataDynamicPrimitive._vtRowData.elementAt(i)._vtRow );
                    newRowData._vtRow.addAll( myDatadynamic_Table2._vtRowData.elementAt(j)._vtRow );
                    vtNewRowData.add(newRowData);  
                } 
            }
            myDataDynamicPrimitive._vtNameColumn = vtNameColumn;
            myDataDynamicPrimitive._vtRowData = vtNewRowData;
        }
        else
        {
            int size = myDatadynamic_Table2._vtNameColumn.size();
            for(int i = 0; i < size; i++)
            {
                String newName = nameTable2 + "." + myDatadynamic_Table2._vtNameColumn.elementAt(i);
                myDatadynamic_Table2._vtNameColumn.setElementAt(newName, i);
            }
            myDataDynamicPrimitive = myDatadynamic_Table2;
        }
        
        return myDataDynamicPrimitive;
    }
    
    public MyRowData convertElementRow_To_RowData(Element element)  
    {
        MyRowData row = new MyRowData();
        NodeList lstNodeData =  element.getChildNodes();
        Vector<String> vtRowData = new Vector<String>();
        int Amount_Node = lstNodeData.getLength();        
        for(int i = 0; i < Amount_Node; i++)
        {
            Node dataColumn = lstNodeData.item(i);
            if(!dataColumn.getNodeName().equals("#text"))
                vtRowData.add( dataColumn.getTextContent() );
        }
        row._vtRow = vtRowData;
        row.ID = element.getAttribute("Id");
        return row;
    }
    
    public MyDataDynamic filterDataDynamicWithConditional(MyDataDynamic myDatadynamic, String operand, String nameColumn_Table1, String nameColumn_Table2, String value )
    {
        int indexColumn_Table1 = -1;
        int indexColumn_Table2 = -1;
        
        String nameTable1 = null;
        String nameTable2 = null;
        String nameColumn1 = null;
        String nameColumn2 = null;
        
        if( nameColumn_Table1 != null && nameColumn_Table1.indexOf(".") > 0)
        {
            String[] lstSplit = nameColumn_Table1.split("\\.");
            nameTable1 = lstSplit[0];
            nameColumn1 = lstSplit[1];
        }
        
        if(nameColumn_Table2 != null && nameColumn_Table2.indexOf(".") > 0)
        {
            String[] lstSplit = nameColumn_Table2.split("\\.");
            nameTable2 = lstSplit[0];
            nameColumn2 = lstSplit[1];        
        }        
        
        int iAmountColumn = myDatadynamic._vtNameColumn.size();
        for(int i = 0; i < iAmountColumn; i++)
        {
            String nameColumn = myDatadynamic._vtNameColumn.elementAt(i);
            if(nameColumn.toLowerCase().equals(nameColumn_Table1.toLowerCase()))
                indexColumn_Table1 = i;
            
            if(nameColumn_Table2 != null && nameColumn.toLowerCase().equals(nameColumn_Table2.toLowerCase()))
            {
                indexColumn_Table2 = i;
            }            
        }
                
        int iAmountRemove = 0;
        int AmountRowData = myDatadynamic._vtRowData.size();
        for(int i = 0; i < AmountRowData; i++)
        {
            int indexReal = i - iAmountRemove;
            if(indexReal < 0)
                indexReal = 0;
            
            MyRowData rowData = myDatadynamic._vtRowData.elementAt(indexReal);
            
            String value_Table1 = rowData.index(indexColumn_Table1);
            String value_Table2 = value;
            if(value_Table2 == null)
                value_Table2 = rowData.index(indexColumn_Table2);                       
            
            switch(operand.charAt(0))
            {
                case MyOperand.iGreater:
                {
                    boolean isNumber = isNumber(value_Table1) | isNumber(value_Table2);
                    boolean bCompare = false;
                    bCompare = (isNumber && Float.parseFloat(value_Table1) > Float.parseFloat(value_Table2))
                                |
                                (!isNumber &&value_Table1.compareTo(value_Table2) > 0 );
                    if(!bCompare)
                    {
                        myDatadynamic._vtRowData.remove(indexReal);   
                        iAmountRemove++;
                    }
                    break;
                }
                    
                case MyOperand.iSmaller:
                {
                    boolean isNumber = isNumber(value_Table1) | isNumber(value_Table2);
                    boolean bCompare = false;
                    bCompare = (isNumber && Float.parseFloat(value_Table1) < Float.parseFloat(value_Table2))
                                |
                                (!isNumber &&value_Table1.compareTo(value_Table2) < 0 );
                    if(!bCompare)
                    {

                        myDatadynamic._vtRowData.remove(indexReal);   
                        iAmountRemove++;
                    }
                    break;
                }
                    
                case MyOperand.iEquivalent:
                {
                    boolean isNumber = isNumber(value_Table1) | isNumber(value_Table2);
                    boolean bCompare = false;
                    bCompare = (isNumber && Float.parseFloat(value_Table1) == Float.parseFloat(value_Table2))
                                |
                                (!isNumber &&value_Table1.compareTo(value_Table2) == 0 );
                    if(!bCompare)
                    {
                        myDatadynamic._vtRowData.remove(indexReal);  
                        iAmountRemove++;
                    }
                    
                    break;
                }
                    
                case MyOperand.iLike:
                {
                    break;
                }
            }
        }        
        return myDatadynamic; 
    }
    
    public MyTable getMyTable(String nameTable)
    {
        MyTable myTable = new MyTable();
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";

            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                return myTable;
            myTable._sTableName = nameTable;
            
            Vector<String> vtNameColumns = new Vector<String>();
            String[] sNameColumns = getAllNameColumn(nameTable);            
            
            for(int i = 0;i < sNameColumns.length; i++)
            {
                vtNameColumns.add( sNameColumns[i] );
                sExpression = "//TABLE[@Name = '" + nameTable + "']"+"/STRUCT/TYPE_COLUMN[@Name = '" + sNameColumns[i] + "']";
                Element element_Types = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
                myTable._vtTypeColumns.add( element_Types.getAttribute("Type") );                                
            }
            myTable._vtNameColumns = vtNameColumns;
            
            
            
        }
        catch(Exception ex)
        {
            
        }
        return myTable;
    }
    
  
    
    public String[] getListNameTable()
    {
        String[] lstNameTable = null;
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE";

            NodeList listNode_Table = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);
            if(listNode_Table == null)
                return lstNameTable;


            int iAmountNode = listNode_Table.getLength();
            lstNameTable = new String[iAmountNode];
            for(int i = 0; i < iAmountNode; i++)
            {
                Element test = (Element)listNode_Table.item(i);
               String nameTable = test.getAttribute("Name");
               lstNameTable[i] = nameTable;
            }            
        }
        catch(Exception ex)
        {
        
        }
        
        return lstNameTable;
    }
    /*
     * DELETE FROM table_name
     */
    public boolean deleteTable(String tableName)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + tableName + "']";                       
            Node node_Table = (Node)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(node_Table == null)
                return false;
            
            Node node_Parent = node_Table.getParentNode();
            node_Parent.removeChild(node_Table);    
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }
    
    public boolean deleteColumn(String nameTable, String nameColumn)
    {
        try
        {
            
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";                       
            Node node_Table = (Node)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(node_Table == null)
                return false;
            
            String formal_NameColumn = nameColumn.toUpperCase();
            ((Element)node_Table).removeAttribute(formal_NameColumn);
            String namePrimaryKey = ((Element)node_Table).getAttribute("Primary_Key");
            if(namePrimaryKey.equals(formal_NameColumn))
            {
                ((Element)node_Table).setAttribute("Primary_Key", "null");
            }
            
            //xoa Tye_Struct Column
            sExpression = "./STRUCT/TYPE_STRUCT[@Name = '" + nameColumn + "']";
            Element element_TypeStruct = (Element)xpath.evaluate(sExpression, node_Table, XPathConstants.NODE);
            Element elementParent_TypeStruct = (Element)element_TypeStruct.getParentNode();
            elementParent_TypeStruct.removeChild(elementParent_TypeStruct);
            
            //xoa data
            sExpression = "//ROW/" + formal_NameColumn;
            NodeList node_RowDatas = (NodeList)xpath.evaluate(sExpression, node_Table, XPathConstants.NODESET);
            int iAmountRow = node_RowDatas.getLength();
            if(iAmountRow > 0)
            {                
                for(int i = iAmountRow - 1; i >= 0; i--)
                {
                    Element element_dl = (Element)node_RowDatas.item(i);
                    Element element_ParentData = (Element)element_dl.getParentNode();                    
                    element_ParentData.removeChild(element_dl);
                }
            }
            
            saveToFile();
            return true;
            
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    
    public boolean renameColumn(String nameTable, String nameColumn, String newNameColumn, String newType)
    {
        try
        {
            
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";                       
            Node node_Table = (Node)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(node_Table == null)
                return false;
            
            newNameColumn = newNameColumn.toUpperCase();
            String formal_NameColumn = nameColumn.toUpperCase();
            ((Element)node_Table).removeAttribute(formal_NameColumn);
            ((Element)node_Table).setAttribute(newNameColumn, newNameColumn);
            
            String namePrimaryKey = ((Element)node_Table).getAttribute("Primary_Key");
            if(namePrimaryKey.equals(formal_NameColumn))
            {                
               ((Element)node_Table).setAttribute("Primary_Key", newNameColumn);
            }
            
            //xoa Tye_Struct Column
            sExpression = "./STRUCT/TYPE_STRUCT[@Name = '" + nameColumn + "']";
            Element element_TypeStruct = (Element)xpath.evaluate(sExpression, node_Table, XPathConstants.NODE);
            Element elementParent_TypeStruct = (Element)element_TypeStruct.getParentNode();
            elementParent_TypeStruct.removeChild(elementParent_TypeStruct);
            //thêm type column mới
            Element element_newTypeStruct = createElementStruct(nameColumn, newType);
            elementParent_TypeStruct.appendChild(element_newTypeStruct);
            
            //xoa data
            sExpression = "//ROW/" + formal_NameColumn;
            NodeList node_RowDatas = (NodeList)xpath.evaluate(sExpression, node_Table, XPathConstants.NODESET);
            int iAmountRow = node_RowDatas.getLength();
            if(iAmountRow > 0)
            {                
                for(int i = iAmountRow - 1; i >= 0; i--)
                {
                    
                    Element element_dl = (Element)node_RowDatas.item(i);
                    String value_element = element_dl.getTextContent();
                    Element element_ParentData = (Element)element_dl.getParentNode();                    
                    element_ParentData.removeChild(element_dl);
                    
                    Element newRow = createElementRow(newNameColumn, value_element);
                    element_ParentData.appendChild(newRow);
                    
                }
            }
            
            saveToFile();
            return true;
            
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    
    private Element createElementStruct(String nameColumn, String type)
    {
        Element element = _doc.createElement("TYPE_COLUMN");
        element.setAttribute("Name", nameColumn);
        element.setAttribute("Type", type);
        return element;
    }
    
    private Element createElementRow(String nameRow, String value)
    {
        Element element = _doc.createElement(nameRow);
        element.setNodeValue(value);       
        return element;
    }
    
    /*
     * DELETE FROM table_name
     * WHERE some_column=some_value
     */
    public boolean deleteDataRow(String nameTable, String id)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            /*
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";                       
            Node node_Table = (Node)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(node_Table == null)
                return false;
            */
            String sExpression = "//TABLE[@Name = '" + nameTable + "']"+"/ROW[@Id = '" + id + "']";           
            Node node_Row = (Node)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(node_Row == null)
                return false;
            
            Node node_Row_Parent = node_Row.getParentNode();
            node_Row_Parent.removeChild(node_Row);
            
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }
    
    /*
     * UPDATE table_name
     * SET column1=value, column2=value2,...
     * WHERE some_column=some_value
     */      
    
    public boolean updateDataTable(String nameTable, Vector<MyRowData> lstRowData)
    {
        boolean bUpdate = true;
        try
        {
            int iAmountRow = lstRowData.size();
          
            for(int i = 0; i < iAmountRow; i++)
            {
                bUpdate = updateRow(nameTable, lstRowData.elementAt(i));
                if(!bUpdate)
                {
                    return false;
                }
            }
        }
        catch(Exception ex)
        {
            return false;
        }        
        return true;
    }
    
    public boolean updateRow(String nameTable, MyRowData rowData)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";
            Element node_Table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(node_Table == null)
                return false;
            
            sExpression = "./ROW[@Id = '" + rowData.ID +"']";
            Element node_Row = (Element)xpath.evaluate(sExpression, node_Table, XPathConstants.NODE);
            
            //neu day chi la update du lieu co san
            if(node_Row != null)
            {
                String[] lstNameColumn = this.getAllNameColumn(nameTable);
                for(int i = 0; i < lstNameColumn.length; i++)
                {
                    sExpression = "./" + lstNameColumn[i];
                    Element element_Data = (Element)xpath.evaluate(sExpression, node_Row, XPathConstants.NODE);                
                    element_Data.setTextContent(rowData._vtRow.elementAt(i));                
                }
            }
            else
            {
                //có 1 hàm chưa viết xong : InserDatarow
                String[] lstNameColumn = this.getAllNameColumn(nameTable);
                Element element_NewRow = _doc.createElement("ROW");
                int iNewID = createID(nameTable);
                element_NewRow.setAttribute("Id", String.valueOf(iNewID));
                
                for(int i = 0; i < lstNameColumn.length; i++)
                {
                    Element element_NewCellData = _doc.createElement(lstNameColumn[i]);
                    element_NewCellData.setTextContent(rowData.index(i));
                    element_NewRow.appendChild(element_NewCellData);
                }
                
                node_Table.appendChild(element_NewRow);
                        
            }    
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }                
        return true;
    }
        
    /*
     * UPDATE Persons
     * SET Address='Nissestien 67', City='Sandnes'
     */
    public boolean updateStructTable(MyStructTable structTable, String nameTable)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";                       
            Element node_Table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(node_Table == null)
                return false;
            
            MyStructTable structTable_Old = getStructTable(nameTable);
            
            String nameColumn = "";
            String newNameColumn = "";
            String newType = "string";
            int iAmountColumn = structTable_Old._vtAttribute.size();
            for(int i = 0; i < iAmountColumn; i++)
            {
                Attribute_Value old_attribute = structTable_Old._vtAttribute.elementAt(i);
                Attribute_Value new_attribute = structTable._vtAttribute.elementAt(i);
                nameColumn = old_attribute.NameAttribute;
                newNameColumn = new_attribute.NameAttribute;
                newType = new_attribute.Type;
                
                boolean bRename = renameColumn(nameTable, nameColumn, newNameColumn, newType);
                if(!bRename)
                    return false;
            }
            
            /*
            boolean bCheckCoinCide = checkCoinCide( nameTable, structTable._sPrimaryKey);
            //nếu tồn tại giá trị trùng --> không thể set primary key cho thuộc tính cột này được
            if(bCheckCoinCide)
                return false;
           
            if(structTable._sPrimaryKey != null)
                node_Table.setAttribute("Primary_Key", structTable._sPrimaryKey);
            else
                node_Table.setAttribute("Primary_Key", "null");
            
            int AmountColumn = structTable._vtAttribute.size();
            for(int i = 0; i < AmountColumn; i++)
            {
                Attribute_Value attribute = structTable._vtAttribute.elementAt(i);
                Attribute_Value attribute_old = structTable_Old._vtAttribute.elementAt(i);
                String new_nameColumn = attribute.NameAttribute;
                String old_nameColumn = attribute_old.NameAttribute;
                String oldStyle = getTypeAttribute(nameTable, old_nameColumn);
                String newStyle = attribute.Type;
                
                if(!oldStyle.equals(newStyle))
                {
                    boolean bCompatible = checkCompatible(nameTable, old_nameColumn, newStyle);
                    if(bCompatible)
                    {
                        sExpression = "//TYPE_COLUMN[@Name = '" + old_nameColumn + "']"; 
                        Element node_Style = (Element)xpath.evaluate(sExpression, node_Table,XPathConstants.NODE);
                        //node_Style.setAttribute("Type", newStyle);
                        renameElement(node_Style, new_nameColumn);                        
                        
                        
                    }                    
                }                
            }*/
            
            saveToFile();
        }
        catch(Exception ex)
        {
        
        }        
        return true;
    }   
    
    private boolean renameElement(Element element, String newNameElement)
    {
        try
        {
            Element element_Parent = (Element)element.getParentNode();
            element_Parent.removeChild(element);
            Element newElement = _doc.createElement(newNameElement);
            element_Parent.appendChild(newElement);
            saveToFile();
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }
    
    public MyStructTable getStructTable(String nameTable)
    {
        MyStructTable structTable = new MyStructTable();
        try
        {            
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']" + "/STRUCT/TYPE_COLUMN";
            NodeList nodes_TypeColumn = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);
            int iAmountNode = nodes_TypeColumn.getLength();
            for(int i = 0; i < iAmountNode; i++)
            {
                Attribute_Value attribute = new Attribute_Value();
                Element element_attribute = (Element)nodes_TypeColumn.item(i);
                attribute.NameAttribute = element_attribute.getAttribute("Name");
                attribute.Type = element_attribute.getAttribute("Type");
                structTable._vtAttribute.add(attribute);                
            }                        
            
            sExpression = "//TABLE[@Name = '" + nameTable + "']";
            Element element_Table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            structTable._sPrimaryKey = element_Table.getAttribute("Primary_Key");
        }
        catch(Exception ex)
        {
            
        }
        return structTable;
    }
    // Move xuống đây cho gọn. Nếu cần xài thì move lên nha. Đừng hỏi vì sao tôi yêu em ..
    
    /*private MyDataDynamic getDataDynamicwithConditional(MyDataDynamic myDataDynamic1, MyDataDynamic myDatadynamic2, String operand, String nameColumn_Table1, String nameColumn_Table2)
    {
        MyDataDynamic myResultData = new MyDataDynamic();
        
        int AmountNameColumn_Table1 = myDataDynamic1._vtNameColumn.size();                       
        int index_NameTable1 = -1;
        for(int i = 0; i < AmountNameColumn_Table1; i++)
        {
            if(myDataDynamic1._vtNameColumn.elementAt(i).equals(nameColumn_Table1))
            {
                index_NameTable1 = i;
                break;
            }
        }
        
        int AmountNamecolumn_Table2 = myDatadynamic2._vtNameColumn.size();
        int index_NameTable2 = -1;
        for(int i = 0; i < AmountNamecolumn_Table2; i++)
        {
            if(myDatadynamic2._vtNameColumn.elementAt(i).equals(nameColumn_Table2))
            {
                index_NameTable2 = i;
                break;
            }
        }                
               
        int AmountRowData_Table1 = myDataDynamic1._vtRowData.size();
        int AmountRowData_Table2 = myDatadynamic2._vtRowData.size();                
        for(int i = 0; i < AmountRowData_Table1; i++)
        {
            for(int j = 0; j < AmountRowData_Table2; j++)
            {                
                MyRowData myRowData = new MyRowData();
                MyRowData rowData_1 = myDataDynamic1._vtRowData.elementAt(i);
                MyRowData rowData_2 = myDatadynamic2._vtRowData.elementAt(j);

                String value_1 = rowData_1.index(index_NameTable1);
                String value_2 = rowData_2.index(index_NameTable2);
                
                switch(operand.charAt(0))
                {
                    case MyOperand.iGreater:
                    {                        
                        boolean isNumber = isNumber(value_2) | isNumber(value_1);
                        if(isNumber && Float.parseFloat(value_1) > Float.parseFloat(value_2) )
                        {                           
                            myRowData._vtRow.addAll( rowData_1._vtRow );
                            myRowData._vtRow.addAll( rowData_2._vtRow );                                                                                    
                        }                        
                        else
                        {
                            if(value_1.compareTo(value_2) > 0)
                            {
                                myRowData._vtRow.addAll( rowData_1._vtRow );
                                myRowData._vtRow.addAll( rowData_2._vtRow );                                                                                    
                            }
                        }
                        break;
                    }
                    case MyOperand.iSmaller:
                    {                        
                        boolean isNumber = isNumber(value_2) | isNumber(value_1);
                        if(isNumber  )
                        {         
                            if( Float.parseFloat(value_1) < Float.parseFloat(value_2))
                            {
                                myRowData._vtRow.addAll( rowData_1._vtRow );
                                myRowData._vtRow.addAll( rowData_2._vtRow );                                                                                    
                            }
                        }
                        else
                        {
                            if(value_1.compareTo(value_2) < 0)
                            {
                                myRowData._vtRow.addAll( rowData_1._vtRow );
                                myRowData._vtRow.addAll( rowData_2._vtRow );                                                                                    
                            }
                        }
                        break;
                    }
                        
                    case MyOperand.iEquivalent:
                    {                        
                        boolean isNumber = isNumber(value_2) | isNumber(value_1);
                        if(value_1 ==  value_2)
                        {                           
                            myRowData._vtRow.addAll( rowData_1._vtRow );
                            //rowData_2._vtRow.remove(index_NameTable2);                            
                            myRowData._vtRow.addAll( rowData_2._vtRow );                                                                                    
                        }                                                
                        break;                    
                    }                                            
                }                
                myResultData._vtRowData.add(myRowData);
            }
        }
        return myResultData;
        //loại bỏ column thừa
                        
    }*/
    
    public String checkExistColumn(String[] lstNameTable, String nameColumn)
    {
        for(int i = 0; i < lstNameTable.length; i++)
        {
            try
            {
                XPath xpath = XPathFactory.newInstance().newXPath();
                String sExpression = "//TABLE[@Name = '" + lstNameTable[i] + "']";

                Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
                if(element_table == null)
                    return null;
            
                NamedNodeMap listNode =element_table.getAttributes(); 
                int iAmountAttribute = listNode.getLength();
                for(int j = 0; j < iAmountAttribute; j++ )
                {
                    String valueAttribute = listNode.item(j).getNodeValue();
                    if(valueAttribute.equals(nameColumn))
                        return element_table.getAttribute("Name");
                }                
            }
            catch(Exception ex)
            {
            
            }
        }
        return null;
    }
    
    public boolean isNumber(String sNumber)
    {
        try
        {
            Float.parseFloat(sNumber);
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }
    
    public boolean checkCompatible(String nameTable, String nameColumn, String newType)
    {
        if(newType.equals(TypeData.FLOAT))
        {
            try
            {
                String[] values = getListValue(nameTable, nameColumn);
                for(int i = 0; i < values.length; i++)
                {
                    Float.parseFloat(values[i]);
                }
            }
            catch(Exception ex)
            {
                return false;
            }               
        }
        return true;
    }
    
    /*kiểm tra sự tồn tại giá trị trùng trong danh sách giá trị input*/
    public boolean checkCoinCide(String nameTable, String nameColumn)
    {
        String[] sValues = getListValue(nameTable, nameColumn);
        return checkValueCoinCide(sValues);
    }
    
    private boolean checkValueCoinCide(String[] sValue)
    {
        if(sValue != null)
        { 
            for(int i = 0; i < sValue.length - 1; i++)
            {
                for(int j = i + 1; j < sValue.length; j++)
                {
                    if(sValue[j] == sValue[i])
                        return true;
                }
            }
        }
        return false;
    }
    
    
    public String[] getListValue(String nameTable, String nameColumn)
    {
        String[] sValue = null;
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";

            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null || nameColumn == null)
                return null;
            
            sExpression = "//ROW/" + nameColumn.toUpperCase();
            NodeList node_Rows =  (NodeList)xpath.evaluate(sExpression, element_table, XPathConstants.NODESET);
            int iAmountNodeRow = node_Rows.getLength();
            sValue = new String[iAmountNodeRow];
            for(int i = 0; i < iAmountNodeRow; i++)
            {
                Element element_Row = (Element)node_Rows.item(i);
                sValue[i] = element_Row.getTextContent();
            }
            
            return sValue;
            
        }
        catch(Exception ex)
        {
            return null;
        }
        
    }
    
    public boolean saveToFile()
    {
         try
         {
            Source source = new DOMSource(_doc);
            // Prepare the output file
            File file = new File(FILE_DATABASE);
            Result result = new StreamResult(file);
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
            xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            xformer.transform(source, result);
         }
        catch(Exception ex)
        {
            return false;
        }
         
        return true;
    }
    
    public MyDataDynamic getDataDynamicWithSingleCondition(String nameTable, String condition)
    {
        MyDataDynamic lstData = new MyDataDynamic();
        
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";

            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                return lstData;
            
            //lấy danh sách tên cột           
            String[] lstNameColumn = getAllNameColumn(nameTable);
            lstData.set_vtNameColumn(lstNameColumn);
            // Tạo đk
            String[] con = new StringHelper().SplitCondition(condition);
            //lấy danh sách dữ liệu
            
            sExpression = "//TABLE[@Name = '" + nameTable + "']"+"/ROW";//+con[0];//[@"+con[0]+" "+con[1]+" '"+con[2]+"']";
            
            NodeList lstNodeData = (NodeList)xpath.evaluate(sExpression, _doc, XPathConstants.NODESET);            
            int iCountData = lstNodeData.getLength();
            for(int i = 0; i < iCountData; i++)
            {
                Element element_Row = (Element)lstNodeData.item(i);
                MyRowData myData = new MyRowData();                    
                boolean bAdd = false;
                NodeList attRow = element_Row.getElementsByTagName(con[0]);
                for(int k = 0; k < attRow.getLength(); k++)
                {
                    if(attRow.item(k).getTextContent() == null)
                        continue;
                    
                    if( con[1].compareTo("=") == 0 && attRow.item(k).getTextContent().equals(con[2]) == true)
                    {
                        bAdd = true;
                    }
                    else
                    if( con[1].compareTo("!=") == 0 && attRow.item(k).getTextContent().equals(con[2]) == false)
                    {
                        bAdd = true;
                    }
                    else
                    if( con[1].compareTo(">=") == 0)
                    {
                        if(isNumber(attRow.item(k).getTextContent()) && isNumber(con[2]))
                        {
                            if(attRow.item(k).getTextContent() == null ? con[2] == null : Float.parseFloat(attRow.item(k).getTextContent()) >= Float.parseFloat(con[2]))
                                bAdd = true;
                        }
                    }
                    else
                    if( con[1].compareTo(">") == 0)
                    {
                        if(isNumber(attRow.item(k).getTextContent()) && isNumber(con[2]))
                        {
                            if(attRow.item(k).getTextContent() == null ? con[2] == null : Float.parseFloat(attRow.item(k).getTextContent()) > Float.parseFloat(con[2]))
                                bAdd = true;
                        }
                    }
                    else
                    if( con[1].compareTo("<=") == 0)
                    {
                        if(isNumber(attRow.item(k).getTextContent()) && isNumber(con[2]))
                        {
                            if(attRow.item(k).getTextContent() == null ? con[2] == null : Float.parseFloat(attRow.item(k).getTextContent()) <= Float.parseFloat(con[2]))
                                bAdd = true;
                        }
                    }
                    else
                    if( con[1].compareTo("<") == 0)
                    {
                        if(isNumber(attRow.item(k).getTextContent()) && isNumber(con[2]))
                        {
                            if(attRow.item(k).getTextContent() == null ? con[2] == null : Float.parseFloat(attRow.item(k).getTextContent()) < Float.parseFloat(con[2]))
                                bAdd = true;
                        }
                    }
                }
                if(bAdd)
                {
                    bAdd = false;
                    lstData._vtRowData.add(convertElementRow_To_RowData(element_Row));
                }
            }           
        }
        catch(Exception ex)
        {
            int a = 4;
            a--;
        }
        
        return lstData;
    }

    public MyDataDynamic mergeDataDynamicAND(MyDataDynamic table1, MyDataDynamic table2)
    {
        MyDataDynamic tableResult  = new MyDataDynamic();
        for(int i = 0; i< table1._vtRowData.size(); i++)
        {
            for(int j = 0; j<table2._vtRowData.size(); j++)
            {
                if(table1._vtRowData.get(i).isSameRow(table2._vtRowData.get(j)) == true)
                {
                    tableResult._vtRowData.add(table2._vtRowData.get(j));
                }
            }
        }
       tableResult._vtNameColumn = MergeNameColumn(table1._vtNameColumn, table2._vtNameColumn);
        return tableResult;
    }
    
    
    public MyDataDynamic mergeDataDynamicOR(MyDataDynamic table1, MyDataDynamic table2)
    {
        MyDataDynamic tableResult  = new MyDataDynamic();
        
        tableResult._vtRowData.addAll(table1._vtRowData);
        tableResult._vtRowData.addAll(table2._vtRowData);
        tableResult._vtNameColumn = MergeNameColumn(table1._vtNameColumn, table2._vtNameColumn);
        //tableResult = tableResult.RemoveSameElement();
        return tableResult;
    }
    
    public Vector<String> MergeNameColumn(Vector<String> nameCol1, Vector<String> nameCol2)
    {
        Vector<String> nameResultTemp = new Vector<String>();
        Vector<String> nameResult = new Vector<String>();
        nameResultTemp.addAll(nameCol1);
        nameResultTemp.addAll(nameCol2);
        
        while(nameResultTemp.size() > 0)
        {
            if(nameResult.contains(nameResultTemp.get(0)) != true)
            {
                nameResult.add(nameResultTemp.get(0));
            }
            nameResultTemp.remove(0);
        }
        
        return nameResult;
    }
    
    public MyDataDynamic mergeDataDynamicAndOr(MyDataDynamic table1, MyDataDynamic table2, String condition)
    {
        MyDataDynamic result = new MyDataDynamic();
        XMLManage xml = new XMLManage();
        if(condition.toUpperCase().equals("AND"))
        {
            result = xml.mergeDataDynamicAND(table1, table2);
        }
        if(condition.toUpperCase().equals("OR"))
        {
            result = xml.mergeDataDynamicOR(table1, table2);
        }
        return result;
    }
    
    /* KHông cần thiết khi row có ID. Cần phải cải thiện lại chỗ Insert sau.
    public Vector<Element> getVectorElementWithSingleCondition(String nameTable, String condition)
    {
        Vector<Element> lstData = new Vector<Element>();
        
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            String sExpression = "//TABLE[@Name = '" + nameTable + "']";

            Element element_table = (Element)xpath.evaluate(sExpression, _doc, XPathConstants.NODE);
            if(element_table == null)
                return lstData;
            
            // Tạo đk
            String[] con = new StringHelper().SplitCondition(condition);
            //lấy danh sách dữ liệu
            
            sExpression = "//ROW";//+con[0];//[@"+con[0]+" "+con[1]+" '"+con[2]+"']";
            
            NodeList lstNodeData = (NodeList)xpath.evaluate(sExpression, element_table, XPathConstants.NODESET);            
            int iCountData = lstNodeData.getLength();
            for(int i = 0; i < iCountData; i++)
            {
                Element element_Row = (Element)lstNodeData.item(i);
              
                boolean bAdd = false;
                NodeList attRow = element_Row.getElementsByTagName(con[0]);
                for(int k = 0; k < attRow.getLength(); k++)
                {
                    if(attRow.item(k).getTextContent() == null ? con[2] == null : attRow.item(k).getTextContent().equals(con[2]))
                    {
                        bAdd = true;
                    }
                }
                
                
                
                if(bAdd)
                {
                    bAdd = false;

                    lstData.add(element_Row);
                }
            }
        }
        catch(Exception ex)
        {
            int a = 4;
            a--;
        }
        
        return lstData;
    }*/

    
}
